package com.longge;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LongestConsecutive {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) return 0;
        List<Integer> sortNums = IntStream.of(nums).sorted().distinct().boxed().collect(Collectors.toList());

        int maxCount = 1, tempCount = 1;
        for (int i = 1; i < sortNums.size(); i++) {
            if (sortNums.get(i) == sortNums.get(i - 1) + 1) {
                tempCount++;
                if (tempCount > maxCount) {
                    maxCount = tempCount;
                }
            } else {
                tempCount = 1;
            }
        }
        return maxCount;
    }
}
